let app = getApp()
Component({
    properties: {
      index:{type:Number},  
      goods_id:{type:Number},  
      type:{type:String}, 
      value:{
           type:Number, 
           observer: function(newVal, oldVal) {
            this.update()
          }
        },
        buynum:{
          type:Number, 
          observer: function(newVal, oldVal) {
           this.updatebuynum()
         }
       }
      },
      data:{
          buynum:1,
          min_buy:0,
          max_buy:0
      },
    lifetimes: {
      attached: function () {
            
       }
      },
    methods: {
      updatebuynum:function(){
        var that=this
        that.setData({
          buynum:that.data.buynum
        })
      },
      numchange:function(e){
        var that=this
        var goods_id=this.data.goods_id
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Goods_getGoodsInfo',
            goods_id:goods_id
          },
          success: function(res) {
            if(res.data.code==301){
              wx.showModal({
                content:res.data.msg,
                showCancel:false,
                success: function (res) {
                }
              })               
              return false
            }else{
              var min_buy=res.data.data.goods.min_buy
              var max_buy=res.data.data.goods.max_buy
              var buynum=e.detail.value
              if(parseInt(min_buy)>parseInt(buynum)){
                if(parseInt(min_buy)>0){
                  wx.showToast({
                    title: '商品最少限购'+min_buy+'件',
                    icon: 'none',
                    duration: 1500
                  })
                  return false
                }
              }
              if(parseInt(buynum)>parseInt(max_buy)){
                if(parseInt(max_buy)>0){
                  wx.showToast({
                    title: '商品最多购买'+max_buy+'件',
                    icon: 'none',
                    duration: 1500
                  })
                  return false
                }
              }
              if(typeof(e.detail.value)!='undefined'&&e.detail.value!='')
              {
                if(parseInt(e.detail.value)>0)
                {
                  that.setData({
                    buynum:parseInt(e.detail.value)
                  })
                }else
                {
                  that.setData({
                    buynum:1
                  })
                }
                if(that.data.type=='check-list'||that.data.type=='confirm_order')
                {
                  var choose=[];
                  choose['index']=that.data.index;
                  choose['buynum']=that.data.buynum;
                  that.triggerEvent ('get_num',choose)
                }
                else{
                  that.triggerEvent ('get_num',that.data.buynum)
                }
              }
            }
          }
        })
        
      },
      reduce_num:function(){
          if(parseInt(this.data.buynum)>1)
          {
            var buynum=parseInt(this.data.buynum)-1
            var min_buy=this.data.min_buy
            var max_buy=this.data.max_buy
            if(parseInt(min_buy)>parseInt(buynum)){
              if(parseInt(min_buy)>0){
                wx.showToast({
                  title: '商品最少限购'+min_buy+'件',
                  icon: 'none',
                  duration: 1500
                })
                return false
              }
            }
            if(parseInt(buynum)>parseInt(max_buy)){
              if(parseInt(max_buy)>0){
                wx.showToast({
                  title: '商品最多购买'+max_buy+'件',
                  icon: 'none',
                  duration: 1500
                })
                return false
              }
            }
            this.setData({
              buynum:buynum
            })
            if(this.data.type=='check-list'||this.data.type=='confirm_order')
            {
              var choose=[];
              choose['index']=this.data.index;
              choose['buynum']=this.data.buynum;
              this.triggerEvent ('get_num',choose)
            }
            else{
              this.triggerEvent ('get_num',this.data.buynum)
            }
          }
      },
      reduce:function(){
        var that=this
        var goods_id=this.data.goods_id
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Goods_getGoodsInfo',
            goods_id:goods_id
          },
          success: function(res) {
            if(res.data.code==301){
              wx.showModal({
                content:res.data.msg,
                showCancel:false,
                success: function (res) {
                }
              })               
              return false
            }else{
              that.setData({
                min_buy:res.data.data.goods.min_buy,
                max_buy:res.data.data.goods.max_buy
              })
              that.reduce_num();
            }
          }
        })
      },
      plus_num:function(){
        var buynum=parseInt(this.data.buynum)+1
        var min_buy=this.data.min_buy
        var max_buy=this.data.max_buy
        if(parseInt(min_buy)>parseInt(buynum)){
          if(parseInt(min_buy)>0){
            wx.showToast({
              title: '商品最少限购'+min_buy+'件',
              icon: 'none',
              duration: 1500
            })
            return false
          }
        }
        if(parseInt(buynum)>parseInt(max_buy)){
          if(parseInt(max_buy)>0){
            wx.showToast({
              title: '商品最多购买'+max_buy+'件',
              icon: 'none',
              duration: 1500
            })
            return false
          }
        }
        this.setData({
          buynum:buynum
        })
        if(this.data.type=='check-list'||this.data.type=='confirm_order')
        {
          var choose=[];
          choose['index']=this.data.index;
          choose['buynum']=this.data.buynum;
          this.triggerEvent ('get_num',choose)
        }
        else{
          this.triggerEvent ('get_num',this.data.buynum)
        }
      },
      plus:function(){
        var that=this
        var goods_id=this.data.goods_id
        console.log(goods_id)
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Goods_getGoodsInfo',
            goods_id:goods_id
          },
          success: function(res) {
            if(res.data.code==301){
              wx.showModal({
                content:res.data.msg,
                showCancel:false,
                success: function (res) {
                }
              })               
              return false
            }else{
              that.setData({
                min_buy:res.data.data.goods.min_buy,
                max_buy:res.data.data.goods.max_buy
              })
              that.plus_num();
            }
          }
        })
      },
      update:function(){
           var that=this
           this.setData({
            value:that.data.value
           })
      }
    }
});
